package com.cxs.controller;

import com.cxs.dto.StudentDTO;
import com.cxs.pojo.Student;
import com.cxs.service.StudentService;
import cxs.utils.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/*
 * @Author:cxs
 * @Date:2021/10/11  18:33
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;


    @GetMapping("/list")
    public Result list(){
        return Result.OK("请求成功",studentService.list());
    }

    @PostMapping("/add")
    public Result addStu(@RequestBody @Valid StudentDTO studentDTO, BindingResult result){
        if(result.hasErrors()){
            return Result.OK(result.getFieldError().getDefaultMessage(),203,null);
        }
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO,student);
        return studentService.add(student);
    }
}
